#!/usr/bin/env python3

import fnmatch
import os
import re
import sys

dirname = sys.argv[1]
build_xml_dirname = sys.argv[2]
outfile = sys.argv[3]

configoptions = {}

# Parse .rst files
matches = []
for root, dirnames, filenames in os.walk(dirname):
    for filename in fnmatch.filter(filenames, "*.rst"):
        matches.append(os.path.join(root, filename))

for filename in matches:
    with open(filename, "rt", encoding="utf-8") as f:
        link = None
        for l in f.readlines():
            if not link:
                match = re.search(r".. _(.*):", l)
                if match:
                    link = match.group(1)
            else:
                for configoption in re.findall(r":decl_configoption:`[A-Z_0-9]+`", l):
                    if configoption not in configoptions:
                        configoptions[configoption] = set()
                    configoptions[configoption].add(":ref:`%s`" % link)

# Parse and edit .xml files
matches = []
for root, dirnames, filenames in os.walk(build_xml_dirname):
    for filename in fnmatch.filter(filenames, "class*.xml"):
        matches.append(os.path.join(root, filename))

for filename in matches:
    output = ""
    with open(filename, "rt", encoding="utf-8") as f:
        link = None
        for l in f.readlines():
            match = re.search(r"<qualifiedname>(.*)</qualifiedname>", l)
            if match:
                link = match.group(1)
            elif link:
                for configoption in re.findall(r":decl_configoption:`[A-Z_0-9]+`", l):
                    if configoption not in configoptions:
                        configoptions[configoption] = set()
                    configoptions[configoption].add(":cpp:func:`%s`" % link)

                for configoption in re.findall(r"configuration option ([A-Z_0-9]+)", l):
                    if configoption not in configoptions:
                        configoptions[configoption] = set()
                    configoptions[configoption].add(":cpp:func:`%s`" % link)
                l = re.sub(
                    r"configuration option ([A-Z_0-9]+)",
                    r"configuration option <verbatim>embed:rst:inline :decl_configoption:`\1`</verbatim><htmlonly>\1</htmlonly><latexonly>\1</latexonly>",
                    l,
                )

                for configoption in re.findall(r"([A-Z_0-9]+) configuration option", l):
                    if configoption not in configoptions:
                        configoptions[configoption] = set()
                    configoptions[configoption].add(":cpp:func:`%s`" % link)
                l = re.sub(
                    r"([A-Z_0-9]+) configuration option",
                    r"<verbatim>embed:rst:inline :decl_configoption:`\1`</verbatim><htmlonly>\1</htmlonly><latexonly>\1</latexonly> configuration option",
                    l,
                )

            output += l
    open(filename, "wt", encoding="utf-8").write(output)


with open(outfile, "wt", encoding="utf-8") as f:
    f.write("..\n")
    f.write(
        "  This file is generated by build_configoptions_index.py. DO NOT EDIT !!!\n"
    )
    f.write("  Do not put in git !!!\n")
    f.write("..\n")
    f.write("\n")

    for key in sorted(configoptions.keys()):
        f.write("* %s:" % key)
        if len(configoptions[key]) >= 2:
            f.write("\n")
            f.write("\n")
            for link in configoptions[key]:
                f.write("  - %s\n" % link)
        else:
            for link in configoptions[key]:
                f.write(" %s\n" % link)
